#include <map>
#include <vector>
#include <string>
#include "smartpipe.h"

using namespace std;
using namespace sp;

void registe(){
    Image::Crop::cropWithInput::registe();
    Image::Crop::cropWithParas::registe();
    Image::Gen::genFromDisk::registe();
    Image::Gen::genFromMemory::registe();
    Image::Resize::letterBoxResize::registe();
    Image::Resize::resize::registe();
    Image::Save::markAndSave::registe();
    Image::Save::save::registe();
    Image::Trans::trans::registe();
    Model::Group::groupByBatch::registe();
    Model::Split::splitByShape::registe();
    Model::Trans::transferToDeviceMemory::registe();
    Model::Trans::transferToHostMemory::registe();
    Model::Yolo::yolo_complete::registe();
    Model::Yolo::yolo_preprocess::registe();
    Model::Yolo::yolo_inference::registe();
    Model::Yolo::yolo_postprocess::registe();
    Model::Retinanet::retinanet_preprocess::registe();
    Model::Retinanet::retinanet_inference::registe();
    Model::Retinanet::retinanet_postprocess::registe();
    Model::LPRnet::lprnet_preprocess::registe();
    Model::LPRnet::lprnet_inference::registe();
    Model::LPRnet::lprnet_postprocess::registe();
    Model::Openpose::openpose_preprocess::registe();
    Model::Openpose::openpose_inference::registe();
    Model::Openpose::openpose_postprocess::registe();
    Tool::Group::groupByRequestId::registe();
    Tool::Split::splitByFlowId::registe();
}

char* DATA_SOURCE;
char* DATA_TARGET;

char* input_video_data_ptr;
char* output_video_data_ptr;

// 共用参数
string video_path = "/data/lx/SmartPipe/data_source/videos/0.mp4"; // 视频路径
string save_path = "/data/lx/SmartPipe/apps/car_license_plate_recognition/output.avi";  // 输出路径
int video_channels = 1;               // 视频路数
long single_cnt = 1000;               // 单路视频帧数
int single_fps = 30;                  // 单路视频帧率
long cnt = single_cnt*video_channels; // 处理帧数


void test(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    // 构造app
    Function* f0 = new Image::Gen::genFromMemory(input_video_data_ptr, cnt, single_fps*video_channels, 3840, 2160);
    Function* f1 = new Image::Trans::trans();
    Function* f2 = new Image::Trans::trans();
    Function* f3 = new Image::Trans::trans();
    Function* f4 = new Image::Trans::trans();
    Function* f5 = new Image::Trans::trans();
    Function* f6 = new Image::Trans::trans();
    Function* f7 = new Image::Trans::trans();
    Function* f8 = new Image::Trans::trans();
    Function* f9 = new Image::Trans::trans();
    Function* f10 = new Image::Trans::trans();
    Function* f11 = new Image::Save::save("", output_video_data_ptr, cnt, 30, 3840, 2160);
    // 连接逻辑关系
    connectOneByOne(12, f0, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11);
    // 部署表
    map<short, vector<Function*>> Fs_map;
    Fs_map[0] = {f0};
    Fs_map[1] = {f1};
    Fs_map[2] = {f2};
    Fs_map[3] = {f3};
    Fs_map[4] = {f4};
    Fs_map[5] = {f5};
    Fs_map[6] = {f6};
    Fs_map[7] = {f7};
    Fs_map[8] = {f8};
    Fs_map[9] = {f9};
    Fs_map[10] = {f10};
    Fs_map[11] = {f11};
    // 构造app
    App app(0, Fs_map, &smm, &gpu_smm);
    // 运行app
    app.check();
    app.printMsg();
    app.init();
    app.run();
    app.waitForComplete();
}

int main(){
    // 注册
    registe();
    // 声明共享内存管理对象
    SharedMemoryManager smm;
    Gpu_SharedMemoryManager gpu_smm;
    // 将视频加载到内存中
    SharedMemoryPool input_video_pool = smm.createSharedMemoryPool("input_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    SharedMemoryPool output_video_pool = smm.createSharedMemoryPool("output_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    input_video_data_ptr = input_video_pool.getDataPtr();
    output_video_data_ptr = output_video_pool.getDataPtr();
    Image::Gen::genFromDisk f_start(video_path, input_video_data_ptr, cnt, 30, 3840, 2160);
    f_start.run();
    // 进行测试
    test(smm, gpu_smm);
    // 检查是否存在内存泄露
    smm.check();
    // 将输出视频保存到磁盘
    // Image::Save::save f_end(save_path, output_video_data_ptr, cnt, 30, 3840, 2160);
    // f_end.run();
}